unit EConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FMTBcd, DB, DBClient, Provider, SqlExpr,  ExtCtrls,
  StdCtrls, DBCtrls, Mask, Menus, Buttons,FileCtrl, ComCtrls,
  ZAbstractRODataset, ZAbstractDataset, ZDataset, EDBImage, ZConnection;

type
  TfmConfig = class(TForm)
    dsConfig: TDataSource;
    PopupMenu1: TPopupMenu;
    AddPicture1: TMenuItem;
    ClearPicture1: TMenuItem;
    qrConfig: TZQuery;
    qrLang: TZReadOnlyQuery;
    pnForm: TPanel;
    Panel1: TPanel;
    sbEdit: TSpeedButton;
    sbPost: TSpeedButton;
    sbCancel: TSpeedButton;
    sbExit: TSpeedButton;
    GroupBox1: TGroupBox;
    lblCompName: TLabel;
    lblCompNameOth: TLabel;
    lblAdd1: TLabel;
    lblAdd1Oth: TLabel;
    lblAdd2: TLabel;
    lblAdd2Oth: TLabel;
    lblAdd3: TLabel;
    lblAdd3Oth: TLabel;
    lblTel: TLabel;
    lblFax: TLabel;
    lblEmail: TLabel;
    lblContact: TLabel;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBEdit4: TDBEdit;
    DBEdit5: TDBEdit;
    DBEdit6: TDBEdit;
    DBEdit7: TDBEdit;
    DBEdit8: TDBEdit;
    DBEdit9: TDBEdit;
    DBEdit10: TDBEdit;
    DBEdit11: TDBEdit;
    DBEdit12: TDBEdit;
    DBEdit13: TDBEdit;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    GroupBox3: TGroupBox;
    lblReport: TLabel;
    sbRepPath: TSpeedButton;
    lblCalendar: TLabel;
    lblShortDate: TLabel;
    edRepPath: TDBEdit;
    DBComboBox1: TDBComboBox;
    DBEdit1: TDBEdit;
    qrAuthorize: TZQuery;
    Label1: TLabel;
    DBComboBox2: TDBComboBox;
    GroupBox2: TGroupBox;
    ckDocno: TDBCheckBox;
    DBComboBox3: TDBComboBox;
    lblForDoc: TLabel;
    ckFixFor: TDBCheckBox;
    DBEdit14: TDBEdit;
    lblLength: TLabel;
    lblFormat: TLabel;
    DBEdit15: TDBEdit;
    lblLang: TLabel;
    DBComboBox4: TDBComboBox;
    lblDayExp: TLabel;
    DBEdit16: TDBEdit;
    Label2: TLabel;
    ZConnection1: TZConnection;
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DBEdit2Enter(Sender: TObject);
    procedure DBEdit2Exit(Sender: TObject);
    procedure AddPicture1Click(Sender: TObject);
    procedure ClearPicture1Click(Sender: TObject);
    procedure sbRepPathClick(Sender: TObject);
    procedure dsConfigDataChange(Sender: TObject; Field: TField);
    procedure sbEditClick(Sender: TObject);
    procedure sbPostClick(Sender: TObject);
    procedure sbExitClick(Sender: TObject);
    procedure sbCancelClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    procedure DataChanged;
    procedure EditingChanged;
    procedure changcaption;    
  public
    { Public declarations }
    procedure WndProc(var Message: TMessage); override;
  end;

var
  fmConfig: TfmConfig;

implementation
uses Emain,dmIdocqment,Utility;
{$R *.dfm}

procedure TfmConfig.FormActivate(Sender: TObject);
begin
  fmMain.FindButtonQuick(TForm(sender).Handle);

  fmMain.pnAppBar.Parent := TForm(Sender);
  fmMain.pnAppTitle.Parent := TForm(Sender);
  //fmMain.lblShow.Caption:= TForm(Sender).Caption;

  TForm(Sender).Left:=fmMain.Left;
  TForm(Sender).Top:=fmMain.Top;
  TForm(Sender).Width :=fmMain.Width;
  TForm(Sender).Height:=fmMain.Height;
end;

procedure TfmConfig.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  fmMain.delete2sbwin(TForm(sender).Handle);
end;

procedure TfmConfig.DBEdit2Enter(Sender: TObject);
begin
  SetBackColor(sender,true);
end;

procedure TfmConfig.DBEdit2Exit(Sender: TObject);
begin
  SetBackColor(sender,false);
end;

procedure TfmConfig.AddPicture1Click(Sender: TObject);
var bmp:tpicture;
begin
  //if not (qrConfig.state in [dsInsert,dsEdit])then exit;
  bmp:=tpicture.create;
  try
    if dm.OpenPictureDialog.Execute then
    begin
      bmp.LoadFromFile(dm.OpenPictureDialog.FileName);
      qrConfig.edit;
      qrConfig.fieldbyname('pict').Assign(bmp);
    end;
  finally
    bmp.free;
  end;
end;

procedure TfmConfig.ClearPicture1Click(Sender: TObject);
begin
  //if not (cdConfig.state in [dsInsert,dsEdit])then exit;
  qrConfig.edit;
  qrConfig.fieldbyname('pict').Assign(nil);
end;

procedure TfmConfig.sbRepPathClick(Sender: TObject);
var sDir:string;
begin
  //if not(cdConfig.state in [dsInsert,dsEdit]) then exit;
  qrConfig.edit;
  if SelectDirectory('Select Report Path','',sDir)then
  begin
    edRepPath.setfocus;
    edRepPath.text:=sDir;
  end;
end;

procedure TfmConfig.dsConfigDataChange(Sender: TObject; Field: TField);
begin
  DataChanged;
  EditingChanged;
end;

procedure TfmConfig.DataChanged;
var
  UpEnable, DnEnable,state_ds: Boolean;
begin
  state_ds:=qrConfig.state in [dsInsert,dsEdit];
  UpEnable := Enabled and qrConfig.Active and not qrConfig.BOF;
  DnEnable := Enabled and qrConfig.Active and not qrConfig.EOF;
  //sbFirst.Enabled := UpEnable and not state_ds;
  //sbPrior.Enabled := UpEnable and not state_ds;
  //sbNext.Enabled := DnEnable and not state_ds;
  //sbLast.Enabled := DnEnable and not state_ds;
  //sbDelete.Enabled := Enabled and zqrUoc_Curr.Active and zqrUoc_Curr.CanModify and not state_ds and not (zqrUoc_Curr.BOF and zqrUoc_Curr.EOF);
end;

procedure TfmConfig.EditingChanged;
var
  CanModify,modival: Boolean;
begin
  CanModify := Enabled and qrConfig.Active and qrConfig.CanModify;
  modival:=qrConfig.state in [dsInsert,dsEdit];
  //sbInsert.Enabled := CanModify and not modival;
  sbEdit.Enabled := CanModify and not modival;
  sbPost.Enabled := CanModify and modival;
  sbCancel.Enabled := CanModify and modival;

  sbExit.Enabled:=sbEdit.Enabled;

  DataChanged;
end;

procedure TfmConfig.sbEditClick(Sender: TObject);
begin
  if dm.qrChk_Right.fieldbyname('posit').asstring<>'A' then
  begin
    check_authorize('edit config');
  end;

  qrConfig.edit;
end;

procedure TfmConfig.sbPostClick(Sender: TObject);
begin
  //if dm.qrChk_Right.fieldbyname('posit').asstring<>'A' then
  begin
    //check_authorize('edit config');
  end;

  qrConfig.Post;
  qrConfig.ApplyUpdates;
end;

procedure TfmConfig.sbExitClick(Sender: TObject);
begin
  close;
end;

procedure TfmConfig.sbCancelClick(Sender: TObject);
begin
  qrConfig.CancelUpdates;
end;

procedure TfmConfig.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=VK_Up then SelectNext(ActiveControl,false,true);
  if Key=VK_Down then SelectNext(ActiveControl,true,true);
end;

procedure TfmConfig.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then SelectNext(ActiveControl,true,true);
end;

procedure TfmConfig.WndProc(var Message: TMessage);
begin
  case Message.Msg Of
    QEventType_CMSetLang : changcaption;
  else
    inherited;
  end;
end;

procedure TfmConfig.changcaption;
begin
  with qrLang do
  begin
    close;
    parambyname('lang').asstring:=dm.languagedef;
    open;
    lblCompName.caption:=Get2Caption(qrLang,'C001','Company Name');
    lblCompNameOth.caption:=Get2Caption(qrLang,'C002','Company Name (Other)');
    lblAdd1.caption:=Get2Caption(qrLang,'C003','Address 1');
    lblAdd1Oth.caption:=Get2Caption(qrLang,'C004','Address 1 (Other)');
    lblAdd2.caption:=Get2Caption(qrLang,'C005','Address 2');
    lblAdd2Oth.caption:=Get2Caption(qrLang,'C006','Address 2 (Other)');
    lblAdd3.caption:=Get2Caption(qrLang,'C007','Address 3');
    lblAdd3Oth.caption:=Get2Caption(qrLang,'C008','Address 3 (Other)');
    lblTel.caption:=Get2Caption(qrLang,'C009','Tel.');
    lblFax.caption:=Get2Caption(qrLang,'C010','FAX.');
    lblEmail.caption:=Get2Caption(qrLang,'C011','E-Mail');
    lblContact.caption:=Get2Caption(qrLang,'C012','Contact');
    TabSheet1.caption:=Get2Caption(qrLang,'C013','General');
    lblReport.caption:=Get2Caption(qrLang,'C014','Report Path');
    lblCalendar.caption:=Get2Caption(qrLang,'C015','Calendar Type');
    lblShortDate.caption:=Get2Caption(qrLang,'C016','Short Date Format');
    ckDocno.caption:=Get2Caption(qrLang,'C017','Auto Document NO#');
    GroupBox2.caption:=Get2Caption(qrLang,'C018','Doc NO#');
    lblForDoc.caption:=Get2Caption(qrLang,'C019','Format Document');
    ckFixFor.caption:=Get2Caption(qrLang,'C020','Fix Format');
    lblLength.caption:=Get2Caption(qrLang,'C021','Length');
    lblFormat.caption:=Get2Caption(qrLang,'C022','Format');
    lblLang.caption:=Get2Caption(qrLang,'C023','Language');
    lblDayExp.caption:=Get2Caption(qrLang,'C024','Days Expire');
  end;

  with dm.qrLang do
  begin
    close;
    parambyname('lang').asstring:=dm.languagedef;
    open;
  end;
  sbEdit.hint:=Get2Caption(dm.qrLang,'C007','Edit Record');
  sbPost.hint:=Get2Caption(dm.qrLang,'C008','Save Record');
  sbCancel.hint:=Get2Caption(dm.qrLang,'C009','Cancel Record');
  sbExit.hint:=Get2Caption(dm.qrLang,'C010','Close Form');
end;

procedure TfmConfig.FormShow(Sender: TObject);
begin
  fmMain.add2sbwin(TForm(sender).Handle,TForm(sender).caption,TForm(sender).name);
  qrConfig.open;
  changcaption;

  with qrAuthorize do
  begin
    close;
    parambyname('loginname').asstring:=dm.users;
    parambyname('rightsgrp').asstring:='CFG';
    open;
  end;
  dm.qrChk_Right.close;
  dm.qrChk_Right.parambyname('loginname').asstring:=dm.users;
  dm.qrChk_Right.open;
end;

end.
